package com.lmaye;

import com.lmaye.task.DetectRequestTask;
import com.lmaye.task.TimedTask;
import com.lmaye.utils.GsonUtils;
import com.lmaye.utils.ReadAppConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * -- 服务应用检测健康监控
 * - 执行主入口（Java 版）
 *
 * @author lmay.Zhou
 * @date 2019/6/6 15:17 星期四
 * @qq 379839355
 * @email lmay@lmaye.com
 */
@Slf4j
@SuppressWarnings("unchecked")
public class App {
    public static final Map<String, Object> APP_PROPERTIES;

    static {
        // 1. 初始化配置文件
        ReadAppConfig appConfig = new ReadAppConfig();
        Map<String, Object> properties = appConfig.readYaml("application.yml");
        log.info("[init] ---> App Properties: {}", GsonUtils.toJson(properties));
        APP_PROPERTIES = (Map<String, Object>) properties.get("app");
    }

    public static void main(String[] args) {
        // 2. 启动定时任务
        Map<String, Object> taskProperties = (Map<String, Object>) APP_PROPERTIES.get("task");
        long initialDelay = Long.parseLong(Objects.isNull(taskProperties.get("initialDelay")) ? "0" : taskProperties.get("initialDelay").toString());
        long minutes = Long.parseLong(Objects.isNull(taskProperties.get("minutes")) ? "3" : taskProperties.get("minutes").toString());
        TimedTask.scheduledThreadPool(new DetectRequestTask(), initialDelay, minutes, TimeUnit.MINUTES);
    }
}
